package com.hy.service.impl;

import com.hy.bean.Dispose;
import com.hy.bean.RespBean;
import com.hy.bean.RespPageBear;
import com.hy.dao.*;
import com.hy.service.ReportPageService;
import com.hy.utils.MyRunnableUtils;
import com.hy.vo.ReportVo;

import java.util.List;

/**
 *
 *
 * @Description: 举报
 * @Author: zhangzhengwei2006
 * @Date: 2024-01-05 20:53
 *
 *
 */
public class ReportPageServiceImpl implements ReportPageService {

    private int num = 0;

    // 查询所有被举报的帖子并分页
    @Override
    public RespPageBear listCard(Integer currentPage, Integer pageSize) throws Exception {
        ReportDao reportDao = new ReportDao();
        RespPageBear pageBear = new RespPageBear();
        List<ReportVo> cardUsers = reportDao.selectCartPage(currentPage, pageSize);
        Long total = reportDao.selectCardTotal();
        pageBear.setData(cardUsers);
        long pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        pageBear.setTotalPages(pageCount);
        pageBear.setCurrentPage(currentPage);
        pageBear.setPageSize(pageSize);
        return pageBear;
    }

    // 查询所有被举报的用户
    @Override
    public RespPageBear listUser(Integer currentPage, Integer pageSize) throws Exception {
        ReportDao reportDao = new ReportDao();
        RespPageBear pageBear = new RespPageBear();
        List<ReportVo> cardUsers = reportDao.selectUserPage(currentPage, pageSize);
        Long total = reportDao.selectUserTotal();
        pageBear.setData(cardUsers);
        long pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        pageBear.setTotalPages(pageCount);
        pageBear.setCurrentPage(currentPage);
        pageBear.setPageSize(pageSize);
        return pageBear;
    }

    // 查询所有被举报的评论
    @Override
    public RespPageBear listComment(Integer currentPage, Integer pageSize) throws Exception {
        ReportDao reportDao = new ReportDao();
        RespPageBear pageBear = new RespPageBear();
        List<ReportVo> cardUsers = reportDao.selectCommentPage(currentPage, pageSize);
        Long total = reportDao.selectCommentTotal();
        pageBear.setData(cardUsers);
        long pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        pageBear.setTotalPages(pageCount);
        pageBear.setCurrentPage(currentPage);
        pageBear.setPageSize(pageSize);
        return pageBear;
    }

    @Override
    public RespBean ReportAndCard(Integer rid, Integer cid, String state) throws Exception {
        ReportDao reportDao = new ReportDao();
        CardDao cardDao = new CardDao();
        CommentDao commentDao = new CommentDao();
        boolean ifComment = false;
        if (cardDao.updateStatuByid(cid, state) > 0) {
            if (state.equals("2")) // 2是作废
                commentDao.deleteByCardId(cid);
            if (reportDao.UpdateRestate(rid) > 0) {
                return RespBean.success("修改成功");
            }
        }
        return RespBean.success("修改失败");
    }

    @Override
    public RespBean ReporAndUserAndDispose(Dispose dispose, Integer rid) throws Exception {
        MyRunnableUtils myRunnableUtils = new MyRunnableUtils();
        Thread t = new Thread(myRunnableUtils);
        UserDao userDao = new UserDao();
        DisposeDao disposeDao = new DisposeDao();
        ReportDao reportDao = new ReportDao();

        if (disposeDao.saveDispose(dispose) > 0) {
            if (userDao.selectByid(dispose.getUid()).equals("0")) {
                if (userDao.updateUserStatusByid(dispose.getUid(), dispose.getDisposeType()) > 0) {
                    if (reportDao.UpdateRestate(rid) > 0) {
                        if (!dispose.getDisposeType().equals("2")) {
                            MyRunnableUtils.isInterrupt = false;
                            if (num == 0) {
                                t.start();
                                num = 1;
                            }
                        }
                        return RespBean.success("操作成功");
                    }
                }
            }else {
                if (reportDao.UpdateRestate(rid) > 0) {
                    return RespBean.success("操作成功");
                }
            }
        }
        return RespBean.success("操作失败");
    }

    // 删除评论
    @Override
    public RespBean ReportAndComment(Integer rid, Integer coid, String state) throws Exception {
        ReportDao reportDao = new ReportDao();
        CommentDao commentDao = new CommentDao();
            if (commentDao.deleteById(coid,state) > 0) {
                if (reportDao.UpdateRestate(rid) > 0) {
                    return RespBean.success("修改成功");
                }
            }
        return RespBean.success("修改失败");
    }
}
